/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servletuser;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import sessionbean.AccountFacadeLocal;
import sessionbean.CMSClassFacadeLocal;
import sessionbean.CMSRoleFacadeLocal;
import sessionbean.SemesterFacadeLocal;
import sessionbean.StaffProfileFacadeLocal;
import sessionbean.StudentProfileFacadeLocal;

import supportlearning.entities.Account;
import supportlearning.entities.CMSClass;
import supportlearning.entities.CMSRole;
import supportlearning.entities.Semester;
import supportlearning.entities.StaffProfile;
import supportlearning.entities.StudentProfile;

/**
 *
 * @author Huy
 */
public class servletuser extends HttpServlet {

    @EJB
    private CMSClassFacadeLocal cMSClassFacade;
    @EJB
    private StaffProfileFacadeLocal staffProfileFacade;
    @EJB
    private CMSRoleFacadeLocal cMSRoleFacade;
    @EJB
    private AccountFacadeLocal accountFacade;

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {

            String action = request.getParameter("action");
            if (action == null) {
                List<Account> lisacc = accountFacade.Alluser();
                request.setAttribute("listacc", lisacc);
                request.getRequestDispatcher("showalluser.jsp").forward(request, response);
            } else if (action.equals("Submit")) //create user
            {
                String firstname = request.getParameter("txtfirstname");
                String lastname = request.getParameter("txtlastname");
                String username = request.getParameter("txtusername");
                String password = request.getParameter("txtpassword");

                String address = request.getParameter("txtaddress");
                String email = request.getParameter("txtemail");
                boolean gender = Boolean.parseBoolean(request.getParameter("drgender"));

                String des = request.getParameter("desstaff");
                Date d1 = Calendar.getInstance().getTime();
                int semid = Integer.parseInt(request.getParameter("semeterid"));
                Account accobj = accountFacade.find(username);
                if (accobj == null) {
                    CMSRole roleobj = cMSRoleFacade.find(2);
                    Account acc = new Account(username, password, firstname, lastname, address, gender, d1, email, gender, roleobj);
                    StaffProfile staffobj = new StaffProfile(username, des, semid);
                    acc.setStaffProfile(staffobj);
                    staffobj.setAccount(acc);
                    accountFacade.create(acc);
                    response.sendRedirect("createStaffSuccess.jsp");
                } else {
                    out.print("Trung User");
                }

            } else if (action.equals("Save")) {
                List<Account> listacc = accountFacade.Alluser();
                for (int i = 0; i < listacc.size(); i++) {
                    Account accfind = accountFacade.find(listacc.get(i).getAccountId());
                    accfind.setStatus(false);
                    accountFacade.edit(accfind);
                }
                String[] id = request.getParameterValues("cbstatus");
                if (id.length != 0) {

                    for (int i = 0; i < id.length; i++) {
                        Account acc = accountFacade.find(id[i]);
                        acc.setStatus(true);
                        accountFacade.edit(acc);

                    }
                    response.sendRedirect("showalluser");
                } else {
                    out.println("<H1>Khong lay dc ID</H1>");
                }
            } else if (action.equals("Sort")) {
                int id = Integer.parseInt(request.getParameter("selectsortbyAdmin"));
                List<Account> lisacc = new ArrayList<Account>();
                if (id == 1) {
                    lisacc = accountFacade.Alluser();
                } else {
                    lisacc = accountFacade.findbyRoleIDAll(id);

                }
                request.setAttribute("listuser", lisacc);
                request.getRequestDispatcher("homeadmin.jsp").forward(request, response);

            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
